package com.hr.controller;


import com.hr.entity.Unit;
import com.hr.service.UnitService;
import com.hr.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hr
 * @since 2021-10-15
 */
@CrossOrigin
@RestController
@RequestMapping("/hr/service/unit")
public class UnitController {

    @Autowired
    UnitService unitService;

    @GetMapping("units")
    public R get_units() {
        List<Unit> units = unitService.list(null);
        return R.ok().data("units", units);
    }

    @GetMapping("unit")
    public R get_unit(String id) {
        Unit unit = unitService.getById(id);
        return R.ok().data("unit", unit);
    }

    @PostMapping("unit")
    public R add_unit(@RequestBody Unit unit) {
        boolean save = unitService.save(unit);
        return save ? R.ok() : R.error();
    }

    @PutMapping("unit")
    public R update_unit(@RequestBody Unit unit) {
        boolean update = unitService.updateById(unit);
        return update ? R.ok() : R.error();
    }

    @DeleteMapping("unit/{id}")
    public R delete_unit(@PathVariable("id") Integer id) {
        boolean delete = unitService.removeById(id);
        return delete ? R.ok() : R.error();
    }

}

